package com.example.basic.web.service.pharm.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.*;
import com.example.basic.web.enums.DataCodeEnum;
import com.example.basic.web.enums.EnableTypeEnum;
import com.example.basic.web.mapper.pharm.PharmMapper;
import com.example.basic.web.model.pharm.Pharm;
import com.example.basic.web.model.pharmZone.PharmZone;
import com.example.basic.web.service.dataCode.DataCodeService;
import com.example.basic.web.service.pharm.PharmService;
import com.example.basic.web.service.pharmZone.PharmZoneService;
import com.example.basic.web.service.role.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class PharmServiceImpl extends BaseEntityServiceImpl<PharmMapper, Pharm> implements PharmService {

    @Resource
    private PharmMapper pharmMapper;

    @Resource
    private DataCodeService dataCodeService;

    @Resource
    private PharmZoneService pharmZoneService;


    /**
     * 保存或者修改
     * @param pharm 模型
     * @return
     */
    @Override
    public Pharm addOrUpdate(Pharm pharm) {
        //参数检验
        notNull(pharm.getPharmName(),"仓库名称不能为空");
        //校验名称是否重复
        LambdaQueryWrapper<Pharm> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Pharm::getPharmName,pharm.getPharmName());
        if (pharm.getId() != null){
            notNull(pharm.getPharmCode(),"仓库编码不能为空");
            lqw.ne(Pharm::getPharmCode,pharm.getPharmCode());
        }
        Pharm detail = pharmMapper.selectOne(lqw);
        if(detail != null){
            throw new ServiceException("当前仓库名称已存在");
        }
        //修改
        if (pharm.getId() != null){
            preUpdate(pharm);
        }
        //新增
        else {
            //生成仓库编码
            String pharmCode = dataCodeService.generateDataCodeNo(DataCodeEnum.PHARM_CODE.getType());
            pharm.setPharmCode(pharmCode);
            preCreate(pharm);
        }

        //生成拼音码五笔码
        generatePyWbCode(pharm);

        this.save(pharm);
        return detail(pharm.getPharmCode());
    }

    /**
     * @create 生成五笔码、拼音码
     * @param pharm 系统用户模型实体类
     */
    private void generatePyWbCode(Pharm pharm) {
        // 获取仓库名
        String pharmName = StringUtils.safeToString(pharm.getPharmName(), "");
        // 生成拼音码
        if("".equals(StringUtils.safeToString(pharm.getPyCode(),"").replaceAll(" ",""))) {
            String userNamePinyin = PinyinUtils.getSimplePinYin(pharmName);
            pharm.setPyCode(userNamePinyin);
        }
        // 生成拼音码
        if("".equals(StringUtils.safeToString(pharm.getWbCode(),"").replaceAll(" ",""))) {
            String userNameWubi = WubiUtils.getSimpleWuBi(pharmName);
            pharm.setWbCode(userNameWubi);
        }
    }

    /**
     * 详情
     * @param pharmCode 仓库编码
     * @return
     */
    @Override
    public Pharm detail(String pharmCode) {
        notNull(pharmCode, "仓库编码不能为空");

        LambdaQueryWrapper<Pharm> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Pharm::getPharmCode,pharmCode);
        return  this.mapper.selectOne(lqw);
    }

    /**
     * 删除
     * @param pharmCode 仓库编码
     * @return
     */
    @Override
    public int delete(String pharmCode) {
        notNull(pharmCode,"仓库编码不能为空");

        Pharm pharm = detail(pharmCode);
        if(pharm == null){
            throw new ServiceException("未查询到当前数据");
        }

        //校验是否存在在用分区
        List<PharmZone> pharmZoneList = pharmZoneService.queryByPharm(pharmCode);

        if (pharmZoneList != null && pharmZoneList.size() > 0){
            throw new ServiceException("当前仓库存在在用分区,不允许删除");
        }

        return pharmMapper.deleteById(pharm.getId());
    }

    /**
     * 启用/禁用
     * @param pharmCode 仓库编码
     * @return
     */
    @Override
    public int enableOrDisable(String pharmCode) {
        notNull(pharmCode,"仓库编码不能为空");

        Pharm pharm = detail(pharmCode);

        if(pharm == null){
            throw new ServiceException("未查询到当前数据");
        }

        //校验是否存在在用分区
        List<PharmZone> pharmZoneList = pharmZoneService.queryByPharm(pharmCode);

        if (pharmZoneList != null && pharmZoneList.size() > 0){
            throw new ServiceException("当前仓库存在在用分区,不允许操作");
        }

        //停用启用
        if(pharm.getEnableFlag().equals(EnableTypeEnum.ON.getType())){
            pharm.setEnableFlag(EnableTypeEnum.OFF.getType());
        }else {
            pharm.setEnableFlag(EnableTypeEnum.ON.getType());
        }

        preUpdate(pharm);

        return pharmMapper.updateById(pharm);
    }

    /**
     * 仓库列表
     * @param queryPage 分页
     * @param searchParam 模糊搜索(拼音码,五笔码,名称,编码)
     * @param isAll 是否查询全部 0-否，1-是
     * @return
     */
    @Override
    public IPage<Pharm> queryList(Page<Pharm> queryPage, String searchParam, String isAll) {
        return pharmMapper.queryList(queryPage,searchParam,isAll);
    }
}
